/*
 *  Copyright (C) 2010 Martin Haulrich <mwh.isv@cbs.dk>
 *
 *  This file is part of the MatrixParser package.
 *
 *  The MatrixParser program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.osdtsystem.matrixparser.data;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Martin Haulrich
 */
public class CONLLIterator implements GraphIterator {
    final static CONLLToken rootToken = rootToken();
    final static Map<String,String> stringCache = new HashMap<String,String>();
    static boolean grow = true;

    final String fn;
    CONLLSentence nextSen;
    BufferedReader input;
    boolean hasNext;

    public CONLLIterator(String fn) throws FileNotFoundException, UnsupportedEncodingException {
        this.fn = fn;
        open();
    }

    public String name() {
        return fn;
    }
    
    public boolean hasNext() {
        return hasNext;
    }

    public CONLLSentence next() {
        CONLLSentence sen = nextSen;
        update();
        return addRoot(sen);
    }

    public static void stopGrowth() {
        grow = false;
    }

    final static String cachedString(String s) {
        String cached = stringCache.get(s);
        if (cached != null)
            return cached;
        if (s != null && grow)
            stringCache.put(s, s);
         return s;
    }

    private void update() {
        try {
            hasNext = true;
            boolean linesRead = false;

            List<CONLLToken> words = new ArrayList<CONLLToken>();
            String line;
            while ((line = input.readLine()) != null) {
                linesRead = true;
                if (line.isEmpty()) {
                    nextSen = new CONLLSentence(words);
                    return;
                } else {
                    String[] tokens = line.split("\t");

                    // Use cached strings to conserve memory
                    for (int i = 0; i < tokens.length; ++i)
                        tokens[i] = cachedString(tokens[i]);

                    // Create word list
                    List<String> lTokens = Arrays.asList(tokens);
                    words.add(new CONLLToken(lTokens));

                }
            }
            if (!linesRead) {
                hasNext = false;
            }

        } catch (IOException ex) {
            Logger.getLogger(CONLLIterator.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void reset() throws IOException {
        close();
        open();

    }

    private void open() throws FileNotFoundException, UnsupportedEncodingException {
        input = new BufferedReader(new InputStreamReader(new FileInputStream(fn), "UTF8"));
//        input = new BufferedReader(new FileReader(fn), "UTF8");
        update();
    }

    public void close() throws IOException {
        input.close();
    }

    public void remove() {
        throw new UnsupportedOperationException("Not supported.");
    }

    public static CONLLSentence addRoot(CONLLSentence sen) {
        List<CONLLToken> newTokens = new ArrayList<CONLLToken>();
        newTokens.add(rootToken);
        newTokens.addAll(sen.words);
        CONLLSentence withRoot = new CONLLSentence(newTokens);
        return withRoot;
    }

    static CONLLToken rootToken() {
        List<String> rootTokens = new ArrayList<String>();
        rootTokens.add("<root>-id");
        rootTokens.add("<root>-form");
        rootTokens.add("<root>-lemma");
        rootTokens.add("<root>-cpos");
        rootTokens.add("<root>-pos");
        rootTokens.add("<root>-feats");
        rootTokens.add("<root>-head");
        rootTokens.add("<root>-deprel");
        rootTokens.add("<root>-phead");
        rootTokens.add("<root>-pdeprel");
        return new CONLLToken(rootTokens);
    }

}
